// pages/me/ImmediateEvaluate.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    express_id:0,
    img_url: [],
    content: '',
    isimg: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    this.setData({
      express_id: options.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  input: function (e) {
    this.setData({
      content: e.detail.value
    });
    if(this.data.content){
      this.setData({
        isimg: false
      })
    }else{
      this.setData({
        isimg: true
      })
    }
  },
  //选择图片
  chooseimage: function () {
    var that = this;
    if (that.data.img_url.length == 0) {
      var count = 3;
    } else {
      var count = 3 - that.data.img_url.length;
    }
    wx.chooseImage({
      count: count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        if (res.tempFilePaths.length > 0) {
          //把每次选择的图push进数组
          var img_url = that.data.img_url;
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            img_url.push(res.tempFilePaths[i])
          }
          that.setData({
            img_url: img_url,
          });
          if(that.data.content){
            that.setData({
              isimg: false
            });
          }
          // console.log(that.data.img_url.length);
          if (that.data.img_url.length >= 3) {
            that.setData({
              hideAdd: 1
            })
          } else {
            that.setData({
              hideAdd: 0
            })
          }
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  //发布按钮事件
  send: function () {
    var that = this;
    var user_id = app.globalData.openid;
    wx.showLoading({
      title: '上传中',
    });
    that.img_upload();
  },
  //图片上传
  img_upload: function () {
    var that = this;
    var img_url = that.data.img_url;
    console.log(img_url);
    var img_url_ok = [];

    for (let i = 0; i < img_url.length; i++) {
      wx.uploadFile({
        //路径填上传图片方法的地址
        url: app.globalData.serverUrl + 'photoupload',
        filePath: img_url[i],
        name: 'file',
        formData: {
          'user': 'test'
        },
        success: function (res) {
          // console.log('上传成功');
          //把上传成功的图片的地址放入数组中
          const resData = JSON.parse(res.data);
          img_url_ok.push(resData.info);
          console.log(resData);
          //如果全部传完，则可以将图片路径保存到数据库 
          if (img_url_ok.length == img_url.length) {
            wx.request({
              //路径为添加到数据库的方法
              url: app.globalData.serverUrl + 'ImmediateEvaluate',
              method: 'post',
              data: {
                express_id:that.data.express_id,
                openid: app.globalData.openid,
                images: img_url_ok,
                content: that.data.content,
              },
              success: function (res) {
                console.log(res);
                if (res.data.status == 1){
                  wx.hideLoading();
                  wx.showToast({
                    title: '评论成功！',
                    icon: 'success',
                    duration: 1000,
                    success: function (res) {
                      // console.log(res);
                      setTimeout(function () {
                        wx.navigateBack({
                          delta: 1   //返回 
                        })
                      }, 1000);
                    }
                  })
                }else{
                  wx.hideLoading();
                  wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: '评论失败！',
                    confirmColor: '#fc5871'
                  });
                }
              }
            })
          }
        }
      })
    }
  }
})